package com.hunttown.mes.impl;

import com.hunttown.mes.convert.AnalysisCacheParamConverts;
import com.hunttown.mes.rpc.api.AnalysisCacheParamRpcService;
import com.hunttown.mes.rpc.domain.AnalysisCacheParamDTO;
import com.hunttown.mes.service.AnalysisCacheParamService;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.domain.query.AnalysisCacheParamDTOQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisCacheParamConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:04:04
 */
@Service
public class AnalysisCacheParamRpcServiceImpl implements AnalysisCacheParamRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisCacheParamRpcServiceImpl.class);

    private final AnalysisCacheParamService analysisCacheParamService;

    @Autowired
    public AnalysisCacheParamRpcServiceImpl(AnalysisCacheParamService analysisCacheParamService) {
        this.analysisCacheParamService = analysisCacheParamService;
    }

    @Override
    public AnalysisCacheParamDTO insert(AnalysisCacheParamDTO analysisCacheParamDTO) {
        try {
            return AnalysisCacheParamConverts.convert(analysisCacheParamService.insert(AnalysisCacheParamConverts.convert(analysisCacheParamDTO)));
        } catch (Exception e) {
            logger.error("AnalysisCacheParam create failed.", e);
            throw new RuntimeException("AnalysisCacheParam create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisCacheParamDTO analysisCacheParamDTO) {
        try {
            return analysisCacheParamService.updateInfoById(AnalysisCacheParamConverts.convert(analysisCacheParamDTO));
        } catch (Exception e) {
            logger.error("AnalysisCacheParam update by id failed.", e);
            throw new RuntimeException("AnalysisCacheParam update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisCacheParamDTOQuery analysisCacheParamDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisCacheParamDTOQuery.getW_id() != null && analysisCacheParamDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisCacheParamDTOQuery.getW_id());
            }

            if (analysisCacheParamDTOQuery.getIds() != null && !analysisCacheParamDTOQuery.getIds().equals("")) {
                query.put("ids", analysisCacheParamDTOQuery.getIds());
            }

            if (analysisCacheParamDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", analysisCacheParamDTOQuery.getStopFlag());
            }

            if (analysisCacheParamDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisCacheParamDTOQuery.getDeleteFlag());
            }

            return analysisCacheParamService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisCacheParam update by query failed.", e);
            throw new RuntimeException("AnalysisCacheParam update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisCacheParamDTO selectById(Integer id) {
        try {
            return AnalysisCacheParamConverts.convert(analysisCacheParamService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisCacheParam select by id failed.", e);
            throw new RuntimeException("AnalysisCacheParam select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisCacheParamDTO selectObjByQuery(AnalysisCacheParamDTOQuery analysisCacheParamDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisCacheParamDTOQuery.getCacheKey() != null && !analysisCacheParamDTOQuery.getCacheKey().equals("")) {
                query.put("cacheKey", analysisCacheParamDTOQuery.getCacheKey());
            }

            if (analysisCacheParamDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", analysisCacheParamDTOQuery.getStopFlag());
            }

            if (analysisCacheParamDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisCacheParamDTOQuery.getDeleteFlag());
            }

            return AnalysisCacheParamConverts.convert(analysisCacheParamService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisCacheParam select obj by query failed.", e);
            throw new RuntimeException("AnalysisCacheParam select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisCacheParamDTO> getListForPage(AnalysisCacheParamDTOQuery analysisCacheParamDTOQuery) {
        Query query = Query.page(analysisCacheParamDTOQuery.getPn(), analysisCacheParamDTOQuery.getPageSize());

        if (analysisCacheParamDTOQuery.getIds() != null && !analysisCacheParamDTOQuery.getIds().equals("")) {
            query.put("ids", analysisCacheParamDTOQuery.getIds());
        }

        if (analysisCacheParamDTOQuery.getCacheKey() != null && !analysisCacheParamDTOQuery.getCacheKey().equals("")) {
            query.put("cacheKey", analysisCacheParamDTOQuery.getCacheKey());
        }

        if (analysisCacheParamDTOQuery.getLikeName() != null && !analysisCacheParamDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisCacheParamDTOQuery.getLikeName());
        }

        if (analysisCacheParamDTOQuery.getOrderBy() != null && analysisCacheParamDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisCacheParamDTOQuery.getOrderBy());
        }

        if (analysisCacheParamDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", analysisCacheParamDTOQuery.getStopFlag());
        }

        if (analysisCacheParamDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", analysisCacheParamDTOQuery.getDeleteFlag());
        }

        List<AnalysisCacheParamDTO> list = AnalysisCacheParamConverts.convert(analysisCacheParamService.selectByQuery(query));
        Page<AnalysisCacheParamDTO> page = Page.from(analysisCacheParamDTOQuery.getPn(), analysisCacheParamDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisCacheParamService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisCacheParamService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisCacheParam delete failed.", e);
            throw new RuntimeException("AnalysisCacheParam delete failed, Please order your code.", e);
        }
    }
}